﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class page_CustomerOutcomeOfSystem : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime dtime = DateTime.Now;
            tbStartTime.Value = dtime.Year.ToString() + "-" + dtime.Month + "-01";
            tbEndTime.Value = dtime.ToString("yyyy-MM-dd");

            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
//        RequestCompetences = Competences.BuildCompetencesList(Competences.SRMX_35);
        RequestLoginPage = "BalanceCenter/CustomerIncomeOfSystem.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DateTime StartTime = Shove._Convert.StrToDateTime(tbStartTime.Value.ToString(), DateTime.Now.ToShortDateString());
        DateTime EndTime = Shove._Convert.StrToDateTime(tbEndTime.Value.ToString(), DateTime.Now.ToShortDateString()).AddDays(1);

        DataTable dt = new DAL.Views.V_CustomerDetails().Open("", "OperatorType in (2,3,4,5,6,7,8,9,10,12,13) and ShopID = " + _Site.ID.ToString() + " and ([DateTime] between '" + StartTime + "' and '" + EndTime + "')", "[DateTime] asc");

        if (dt == null)
        {
            Shove._Web.JavaScript.Alert(this.Page, "数据库连接失败。");

            return;
        }

        g.DataSource = dt.DefaultView;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void ibtnSelect_Click(object sender, ImageClickEventArgs e)
    {
        gPager.PageIndex = 0;

        BindData();
    }

    protected void ibPrint_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Write("<script language='javascript'>window.open('CustomerIncomeOfSystemForPrint.aspx?StartTime=" + tbStartTime.Value.ToString() + "&EndTime=" + tbEndTime.Value.ToString() + "');</script>");
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            double money = Shove._Convert.StrToDouble(e.Item.Cells[3].Text, 0);
            e.Item.Cells[3].Text = (money == 0) ? "" : money.ToString("N");

            int num = (gPager.PageIndex * gPager.PageSize) + e.Item.ItemIndex + 1;
            e.Item.Cells[0].Text = num.ToString(); 
        }
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        DateTime StartTime = Shove._Convert.StrToDateTime(tbStartTime.Value.ToString(), DateTime.Now.ToShortDateString());
        DateTime EndTime = Shove._Convert.StrToDateTime(tbEndTime.Value.ToString(), DateTime.Now.ToShortDateString()).AddDays(1);

        DataTable dt = new DAL.Views.V_CustomerDetails().Open("isnull(Count([ID]), 0) as [Count], Sum(isnull([Money], 0)) as SumMoney", "OperatorType in (2,3,4,5,6,7,8,9,10,12,13) and ShopID = " + _Site.ID.ToString() + " and ([DateTime] between '" + StartTime + "' and '" + EndTime + "')", "");

        if (dt == null)
        {
            Shove._Web.JavaScript.Alert(this.Page, "数据库连接失败。");

            return;
        }

        if (dt.Rows.Count > 0)
        {
            output.Write("<td align='center'>总计</td>");
            output.Write("<td align='center'>" + dt.Rows[0]["Count"] + "</td>");
            output.Write("<td align='center'>总额</td>");
            output.Write("<td align='center'>" + Shove._Convert.StrToDouble(dt.Rows[0]["SumMoney"].ToString(), 0).ToString("N") + "</td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }
}
